package com.iptv.grape.ui.control.ad;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.iptv.grape.R;
import com.iptv.grape.bu.GrapeContext;
import com.iptv.grape.domain.BillAppointment;
import com.iptv.grape.domain.ChannelBill;
import com.iptv.grape.domain.ChannelType;
import com.iptv.grape.domain.IListViewShowItem;
import com.iptv.grape.domain.ListViewStyle;
import com.iptv.grape.domain.TopMenu;

import java.util.ArrayList;
import java.util.List;

public class ListViewAdapter extends BaseAdapter {
    private LayoutInflater mInflater;
    private List<IListViewShowItem> dataList = new ArrayList<IListViewShowItem>();
    private ListViewStyle style;
    private Context context;

    //public ListViewAdapter clone(){
        //return new ListViewAdapter(mInflater);
    //}

    public List<IListViewShowItem> getDataList() {
        return dataList;
    }

    public void clear(){
        this.dataList.clear();
        this.notifyDataSetChanged();
    }

    public void setDataList(List<IListViewShowItem> dataList, ListViewStyle style) {
        this.dataList.clear();
        this.dataList.addAll(dataList);
        this.style = style;
        this.notifyDataSetChanged();
    }

    public ListViewAdapter(LayoutInflater inflater,Context context){
        this.mInflater = inflater;
        this.context = context;
    }
    @Override
    public int getCount() {
        if (this.dataList == null)
        {
            return 0;
        }
        return this.dataList.size();
    }

    public int getMaxItemsInOneScreen() {
        return 7;
    }

    @Override
    public Object getItem(int position) {
        if(position >= this.getCount()){
            return null;
        }
        return dataList.get(position);
    }

    public IListViewShowItem getListViewShowItem(int position) {
        if(position >= this.getCount()){
            return null;
        }
        return dataList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup viewGroup) {
        convertView = getViewHelp();
        if(getItem(position) == null){
            return convertView;
        }
        //TextView textView = convertView.findViewById(R.id.show);
        //textView.setText(getListViewShowItem(position).getText());
        initItemUI(convertView,position);
        return convertView;
    }

    private View getViewHelp(){
        switch (this.style.getLayoutFileName()){
            case "topMenuListItem":
                return mInflater.inflate(R.layout.list_view_item_topmenu, null);
            case "channelTypeListItem":
                return mInflater.inflate(R.layout.list_view_item_topmenu, null);
            case "channelListItem":
                return mInflater.inflate(R.layout.channel_list_item, null);
            case "channelBillListItem":
                return mInflater.inflate(R.layout.channel_bill_list_item, null);
            case "billAppointment":
                return mInflater.inflate(R.layout.bill_appointment, null);
        }
        return mInflater.inflate(R.layout.list_view_item_topmenu, null);
    }

    private void initItemUI(View convertView,int position){
        switch (this.style.getLayoutFileName()){
            case "topMenuListItem":
                showTextViewUI(convertView,position);
                break;
            case "channelTypeListItem":
                showTextViewUI(convertView,position);
                break;
            case "channelListItem":
                showTextViewUI(convertView,position);
                break;
            case "channelBillListItem":
                showBill(convertView,position);
                break;
            case "billAppointment":
                showBillAppointment(convertView,position);
                break;
        }
    }

    private void showTextViewUI(View convertView,int position){
        TextView textView = convertView.findViewById(R.id.show);
        textView.setText(getListViewShowItem(position).getText());
    }

    private void showBill(View convertView,int position){
        TextView textView = convertView.findViewById(R.id.tv_item_tip1);
        TextView textView2 = convertView.findViewById(R.id.tv_item_tip2);
        TextView textView3 = convertView.findViewById(R.id.tv_item_tip3);

        ChannelBill bill = (ChannelBill)getListViewShowItem(position);

        String billType = bill.getBillType();
        textView2.setText(bill.getName());
        textView3.setText(bill.getShowTimeText());
        if(billType == "now"){
            // 现在
            textView.setText("直播");
            textView.setTextColor(context.getResources().getColor(R.color.item_bill2));
        }
        if(billType == "future"){
            //
            if(bill.isAppointment()){
                textView.setText("已预约");
            }
            else {
                textView.setText("预约");
            }
            textView.setTextColor(context.getResources().getColor(R.color.item_bill3));
            textView.setBackgroundResource(R.drawable.yy_item_backg);
        }
        if(billType == "before"){
            // 现在
            textView.setText("回放");
            textView.setTextColor(context.getResources().getColor(R.color.item_bill1));
            textView.setBackgroundResource(R.drawable.hf_item_backg);
        }

    }

    private void showBillAppointment(View convertView,int position){
        TextView textView = convertView.findViewById(R.id.tv_item_tip1);
        TextView textView2 = convertView.findViewById(R.id.tv_item_tip2);
        TextView textView3 = convertView.findViewById(R.id.tv_item_tip3);

        BillAppointment billAppointment = (BillAppointment)getListViewShowItem(position);

        textView2.setText(billAppointment.getChannelBill().getName());
        textView3.setText(billAppointment.getChannel().getText());
        if(billAppointment.isCancel()){
            textView.setText("已取消");
        }
        else {
            textView.setText("已预约");
        }
    }
}
